package org.jeecg.modules.demo_test;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.google.common.collect.Lists;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.demo_test.service.TestService;
import org.jeecg.modules.demo_test.service.impl.TestServiceImpl;
import org.jeecgframework.codegenerate.database.DbReadTableUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.SQLException;
import java.util.*;

@RestController()
@RequestMapping({"/test/cgform/head"})
public class Controller {
    @Autowired
    TestService testService;
    private static final List<String> b = Lists.newArrayList(new String[]{"act_", "ext_act_", "design_", "onl_", "sys_", "qrtz_"});

    @GetMapping({"/queryTables"})
    public Result<?> a(HttpServletRequest var1) {
        String var2 = JwtUtil.getUserNameByToken(var1);
        if (!"admin".equals(var2)) {
            return Result.error("noadminauth");
        } else {

            List<String> var3;
            String sql;

            try {
                sql = Handle.getSql("oracle");
                updateDs();
                var3 = testService.getTableNames(sql);
            } catch (Exception var9) {
                throw var9;
            }


        /*    org.jeecg.modules.online.cgform.c.b.b(var3);
          //  List var4 = this.onlCgformHeadService.queryOnlinetables();
         //   var3.removeAll(var4);8
         */
            ArrayList var5 = new ArrayList();
            Iterator var6 = var3.iterator();

            while(var6.hasNext()) {
                String var7 = (String)var6.next();
                if (!this.i(var7)) {
                    HashMap var8 = new HashMap();
                    var8.put("id", var7);
                    var5.add(var8);
                }
            }

            return Result.ok(var5);
        }
    }
    private boolean i(String var1) {
        Iterator var2 = b.iterator();

        String var3;
        do {
            if (!var2.hasNext()) {
                return false;
            }

            var3 = (String)var2.next();
        } while(!var1.startsWith(var3) && !var1.startsWith(var3.toUpperCase()));

        return true;
    }
    public void updateDs() {
        try {
            Method method = null;
            method = TestServiceImpl.class.getMethod("getTableNames",String.class);
            DS ds = method.getAnnotation(DS.class);
            if (ds == null)
                throw new RuntimeException("please add testA");
            InvocationHandler invocationHandler = Proxy.getInvocationHandler(ds);
            Field value = null;
            value = invocationHandler.getClass().getDeclaredField("memberValues");
            value.setAccessible(true);
            Map<String, Object> memberValues = (Map<String, Object>) value.get(invocationHandler);
            String val = (String) memberValues.get("value");
            System.out.println("改变前：" + val);
            val = "b";
            memberValues.put("value", val);
            System.out.println("改变后：" + ds.value());
        }catch (Exception e){
                 e.printStackTrace();
        }


    }
}
